package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.CheckinAssessGroup;
import com.api.shopapi.entity.CheckinAssessItem;
import com.api.shopapi.mapper.CheckinAssessGroupMapper;
import com.api.shopapi.service.CheckinAssessGroupService;
import com.api.shopapi.service.CheckinAssessItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 */
@Service("CheckinAssessGroupServiceImplOne")
public class CheckinAssessGroupServiceImpl implements CheckinAssessGroupService {
    @Autowired
    private CheckinAssessGroupMapper checkinAssessGroupMapper;

    @Autowired
    @Qualifier("CheckinAssessItemServiceImplOne")
    private CheckinAssessItemService checkinAssessItemService;

    @Override
    @Transactional
    public int insert(CheckinAssessGroup checkinAssessGroup, JSONArray assessItemArr) {
        String checkinAssessGroupCode = checkinAssessGroup.getCheckinAssessGroupCode();

        int maxFraction = addCheckinAssessItem(checkinAssessGroupCode,assessItemArr);

        checkinAssessGroup.setMaxFraction(maxFraction);
        int count= checkinAssessGroupMapper.insert(checkinAssessGroup);
        return count;
    }

    @Override
    @Transactional
    public int updateByCheckinAssessGroupCode(CheckinAssessGroup checkinAssessGroup,JSONArray assessItemArr) {
        String checkinAssessGroupCode = checkinAssessGroup.getCheckinAssessGroupCode();
        checkinAssessItemService.deleteByCheckinAssessGroupCode(checkinAssessGroupCode);
        int maxFraction = addCheckinAssessItem(checkinAssessGroupCode,assessItemArr);
        checkinAssessGroup.setMaxFraction(maxFraction);
        return checkinAssessGroupMapper.updateByCheckinAssessGroupCode(checkinAssessGroup);
    }

    public int addCheckinAssessItem(String checkinAssessGroupCode, JSONArray assessItemArr){
        int maxFraction = 0;
        JSONObject jsonObject = null;
        CheckinAssessItem checkinAssessItem = new CheckinAssessItem();
        for(Object itme:assessItemArr){
            jsonObject = (JSONObject) itme;
            String itemName = jsonObject.getString("itemName");
            int fraction = jsonObject.getInteger("fraction");
            if(fraction>maxFraction){
                maxFraction = fraction;
            }
            checkinAssessItem.setCheckinAssessItemCode(UUID.randomUUID().toString());
            checkinAssessItem.setCheckinAssessGroupCode(checkinAssessGroupCode);
            checkinAssessItem.setName(itemName);
            checkinAssessItem.setFraction(fraction);
            checkinAssessItemService.insert(checkinAssessItem);
        }
        return maxFraction;
    }

    @Override
    public int deleteByCheckinAssessGroupCode(String checkinAssessGroupCode) {
        return checkinAssessGroupMapper.deleteByCheckinAssessGroupCode(checkinAssessGroupCode);
    }


    @Override
    public CheckinAssessGroup getByCheckinAssessGroupCode(String checkinAssessGroupCode) {
        return checkinAssessGroupMapper.getByCheckinAssessGroupCode(checkinAssessGroupCode);
    }

    @Override
    public CheckinAssessGroup getByNameAndInstitutionCode(String name,String institutionCode) {
        return checkinAssessGroupMapper.getByNameAndInstitutionCode(name,institutionCode);
    }

    @Override
    public PageInfo<CheckinAssessGroup> getPageList(int pageIndex, int pageSize, String institutionCode,int status,String checkinAssessClassCode,String name) {
        PageHelper.startPage(pageIndex, pageSize);
        List<CheckinAssessGroup> checkinAssessGroups = checkinAssessGroupMapper.getListByInstitutionCode(institutionCode,status,checkinAssessClassCode,name);
        PageInfo<CheckinAssessGroup> pageInfo = new PageInfo<>(checkinAssessGroups);
        return pageInfo;
    }

    @Override
    public List<CheckinAssessGroup> getList(String institutionCode) {
        return checkinAssessGroupMapper.getList(institutionCode);
    }

    @Override
    public List<CheckinAssessGroup> getByCheckinAssessClassCode(String checkinAssessClassCode) {
        return checkinAssessGroupMapper.getByCheckinAssessClassCode(checkinAssessClassCode);
    }


    @Override
    public
    PageInfo<CheckinAssessGroup> pageByGovCode(int pageIndex, int pageSize, String governmentCode,
                                               int status,String checkinAssessClassCode,String name){
        PageHelper.startPage(pageIndex, pageSize);
        List<CheckinAssessGroup> checkinAssessGroups = checkinAssessGroupMapper.getListByGovCode(governmentCode,status,checkinAssessClassCode,name);
        PageInfo<CheckinAssessGroup> pageInfo = new PageInfo<>(checkinAssessGroups);
        return pageInfo;
    }

}
